package com.xxwu.messagequeue.mqserver.datacenter;

import com.xxwu.messagequeue.MessageQueueApplication;
import com.xxwu.messagequeue.mqserver.mapper.MetaMapper;
import com.xxwu.messagequeue.mqserver.core.Binding;
import com.xxwu.messagequeue.mqserver.core.Exchange;
import com.xxwu.messagequeue.mqserver.core.ExchangeType;
import com.xxwu.messagequeue.mqserver.core.MSGQueue;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;

/**
 * 创建一个类对数据库操作进行整合
 * 但这个类后续要由我们自行管理，不是 Spring 中的 Bean，所以这里要手动获取 metaMapper
 */
@Slf4j
public class DataBaseManager {


    private MetaMapper metaMapper;

    // 初始化数据库
    public void init() {
        // 手动引入 Bean 对象
        this.metaMapper = MessageQueueApplication.context.getBean(MetaMapper.class);
        //判断数据库是否存在
        if(checkDataBaseExit()){
            // 这里需要将 data 目录先创建出来，否则创建数据表时会报错
            File fileDir = new File("./data");
            fileDir.mkdirs();
            // 创建数据表
            createTable();
            // 插入默认数据
            insertDefault();
            log.info("[DataBaseManager] 数据库初始化完成！");
        } else {
            log.info("[DataBaseManager] 数据库已存在！");
        }
    }

    public void insertDefault() {
        // 在RabbitMQ中这里会插入一个默认的 Direct 交换机
        Exchange exchange = new Exchange();
        exchange.setType(ExchangeType.Direct);
        exchange.setName("");
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertIntoExchange(exchange);
        log.info("[DataBaseManager] 创建初始交换机数据完成!");
    }

    public void deleteDataBase() {
        File file = new File("./data/meta.db");
        boolean delete = file.delete();
        if(delete){
            log.info("[deleteDataBase] 删除数据库成功！");
        } else {
            log.error("[deleteDataBase] 删除数据库失败！");
        }

        // 删除目录前要保证目录是空的，否则会删除失败
        // 将 data 目录也进行删除
        File fileDir = new File("./data");
        boolean result = fileDir.delete();
        if(result){
            log.info("[deleteDataBase] 删除数据库目录成功！");
        } else {
            log.error("[deleteDataBase] 删除数据库目录失败！");
        }
    }

    public void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createBindingTable();
        metaMapper.createMSGQueueTable();
        log.info("[DataBaseManager] 数据表创建完成！");
    }

    public boolean checkDataBaseExit() {
        File file = new File("./data/meta.db");
        if(file.exists()){
            return false;
        } else {
            return true;
        }
    }
    public void insertIntoExchange(Exchange exchange){
        Integer result = metaMapper.insertIntoExchange(exchange);
        log.info("[insertIntoExchange]的结果是：{}",result);
    }
    public List<Exchange> selectExchanges(){
        return metaMapper.selectExchanges();
    }
    public void deleteExchange(String exchangeName){
        Integer result = metaMapper.deleteExchange(exchangeName);
        log.info("[deleteExchange]的结果是：{}",result);
    }
    public void insertIntoBinding(Binding binding){
        Integer result = metaMapper.insertIntoBinding(binding);
        log.info("[insertIntoBinding]的结果是：{}",result);
    }
    public List<Binding> selectBindings(){
        return metaMapper.selectBindings();
    }
    public void deleteBinding(Binding binding){
        Integer result = metaMapper.deleteBinding(binding);
        log.info("[deleteBinding]的结果是：{}",result);
    }
    public void insertIntoMSGQueue(MSGQueue msgQueue){
        Integer result = metaMapper.insertIntoMSGQueue(msgQueue);
        log.info("[insertIntoMSGQueue]的结果是：{}",result);
    }
    public List<MSGQueue> selectMSGQueues(){
        return metaMapper.selectMsgQueues();
    }
    public void deleteMSGQueue(String msgQueueName){
        Integer result = metaMapper.deleteMSGQueue(msgQueueName);
        log.info("[deleteMSGQueue]的结果是：{}",result);
    }
}
